package kr.or.kead.ui.insert_update;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import kr.or.kead.domain.Auth;
import kr.or.kead.domain.Course;
import kr.or.kead.domain.InfoStudent;
import kr.or.kead.domain.Professor;
import kr.or.kead.domain.RequestCourse;
import kr.or.kead.service.DaoCourse;
import kr.or.kead.service.DaoInfoStudent;
import kr.or.kead.service.DaoRequestCourse;
import kr.or.kead.ui.MainFrame;

public class RequestCourseInsetUpdate extends AbsInsertUpdate {
	private JLabel lblNewLabel;
	private JLabel courseIdx;
	private JLabel lblNewLabel_2;
	private JComboBox comboStdCode;
	private JLabel lblNewLabel_3;
	private JComboBox comboCourseCode;
	private JLabel lblNewLabel_4;
	private JTextField txtGrade;
	private Auth auth;
	private DaoRequestCourse daoTable;
	private DaoInfoStudent daostd;
	private DaoCourse daoCourse;
	private String email = "";
	private int level = 0;
	
	public RequestCourseInsetUpdate(Object obj) {
		super(obj, "수강관리");
	}
	@Override
	protected void initDao() {
		auth= MainFrame.getAuth();
		daoTable = new DaoRequestCourse();
		daostd = new DaoInfoStudent();
		daoCourse = new DaoCourse();
		email = auth.getEmail();
		level = auth.getLevel();
	}

	@Override
	protected boolean isValidCheck() {return false;}

	@Override
	protected JPanel getMainPanel() {
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(4, 2, 0, 0));
		
		lblNewLabel = new JLabel("수강코드 :");
		lblNewLabel.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel);
		
		int code = daoTable.selectMaxCode()+1;
		courseIdx = new JLabel(String.valueOf(code));
		centerPanel.add(courseIdx);
		
		lblNewLabel_2 = new JLabel("학생이름 :");
		lblNewLabel_2.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_2);
		
		ArrayList<String> arTeachers = new ArrayList<>();
		if(level == 1){
			InfoStudent std = (InfoStudent)daostd.selectTableByEmail(auth.getEmail());
			arTeachers.add(std.getIdx() + ":" + std.getStdName());
		}else {
			arTeachers = daostd.selectTableAllList();
		}		
		Object[] teachers = arTeachers.toArray();
		comboStdCode = new JComboBox<Object>(teachers);
		centerPanel.add(comboStdCode);
		
		lblNewLabel_3 = new JLabel("강좌이름 :");
		lblNewLabel_3.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_3);
		
		ArrayList<String> courses = daoCourse.selectTableAllList();
		courses.add(0, "강의 선택");
		Object[] data = courses.toArray();
		comboCourseCode = new JComboBox<Object>(data);
		centerPanel.add(comboCourseCode);
		
		lblNewLabel_4 = new JLabel("점    수  :");
		lblNewLabel_4.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_4);
		
		txtGrade = new JTextField();
		centerPanel.add(txtGrade);
		txtGrade.setColumns(10);
		return centerPanel;
	}

	@Override
	protected Object getObject() {
		StringTokenizer namest = new StringTokenizer((String)comboCourseCode.getSelectedItem(),":");
		int nameCode = Integer.parseInt(namest.nextToken().trim());
		StringTokenizer coursest = new StringTokenizer((String)comboCourseCode.getSelectedItem(),":");
		int courseCode = Integer.parseInt(coursest.nextToken().trim());
		if (obj==null){
			RequestCourse request = new RequestCourse(
					Integer.parseInt(courseIdx.getText()), nameCode,courseCode,Integer.parseInt(txtGrade.getText()));
			return request;
		}else{
			RequestCourse request=(RequestCourse)obj;
			request.setIdx(Integer.parseInt(courseIdx.getText()));
			request.setStd_idx(nameCode);
			request.setCourse_code(courseCode);
			request.setGrade(Integer.parseInt(txtGrade.getText()));
			return request;
		}
	}

	@Override
	protected void updateInit() {
		
	}

}
